package com.ionehe.group.common.uaa.ex.domain.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.application.converter.RoleInfoConvert;
import com.ionehe.group.common.uaa.ex.application.model.RoleQuery;
import com.ionehe.group.common.uaa.ex.model.UaaRoleFunction;
import com.ionehe.group.common.uaa.ex.model.UaaRoleInfo;
import com.ionehe.group.common.uaa.ex.dto.Paging;
import com.ionehe.group.common.uaa.ex.dto.RoleInfoDTO;
import com.ionehe.group.common.uaa.ex.dao.RoleFunctionDao;
import com.ionehe.group.common.uaa.ex.dao.RoleInfoDao;
import com.ionehe.group.common.uaa.ex.domain.service.RoleInfoReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @USER:秀
 * @DATE: 2020/11/30
 * @PROJECT_NAME: general-permissions
 * @Description 角色读服务类
 **/
@Service
public class RoleInfoReadServiceImpl implements RoleInfoReadService {

    @Autowired(required = false)
    private RoleInfoDao roleInfoDao;

    @Autowired(required = false)
    private RoleFunctionDao roleFunctionDao;

    @Autowired(required = false)
    private RoleInfoConvert roleInfoConvert;

    @Override
    public Response<List<RoleInfoDTO>> list(Long customerId) {
        //查询角色
        UaaRoleInfo roleInfoQuery = new UaaRoleInfo();
        roleInfoQuery.setCustomerId(customerId);
        roleInfoQuery.setStatus(1);
        roleInfoQuery.setIsDelete(false);
        List<UaaRoleInfo> roleInfoList = roleInfoDao.select(roleInfoQuery);
        if (CollectionUtils.isEmpty(roleInfoList)) {
            return Response.yes(new ArrayList());
        }
        //查询角色关联权限
        List<Long> roleIdList = roleInfoList.stream().map(UaaRoleInfo::getId).collect(Collectors.toList());
        Example example = new Example(UaaRoleFunction.class);
        example.createCriteria().andIn("roleId", roleIdList);
        List<UaaRoleFunction> roleFunctionList = roleFunctionDao.selectByExample(example);
        Map<Long, List<Long>> roleFunctionIdMap = new HashMap();
        roleFunctionList.forEach(roleFunction -> {
            Long roleId = roleFunction.getRoleId();
            List<Long> functionIdList = roleFunctionIdMap.get(roleId);
            if (CollectionUtils.isEmpty(functionIdList)) {
                functionIdList = new ArrayList();
                roleFunctionIdMap.put(roleId, functionIdList);
            }
            functionIdList.add(roleFunction.getFunctionId());
        });
        List<RoleInfoDTO> roleInfoDTOList = new ArrayList(roleInfoList.size());
        roleInfoList.forEach(roleInfo -> {
            RoleInfoDTO roleInfoDTO = roleInfoConvert.do2dto(roleInfo);
            roleInfoDTO.setFunctionIds(roleFunctionIdMap.get(roleInfoDTO.getId()));
            roleInfoDTOList.add(roleInfoDTO);
        });
        return Response.yes(roleInfoDTOList);
    }

    @Override
    public Response<Paging<RoleInfoDTO>> page(RoleQuery roleQuery) {
        //查询角色
        String roleName = roleQuery.getRoleName();
        Date createTimeStart = roleQuery.getCreateTimeStart();
        Date createTimeEnd = roleQuery.getCreateTimeEnd();
        Integer status = roleQuery.getStatus();

        Example example = new Example(UaaRoleInfo.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("customerId", roleQuery.getCustomerId())
                .andEqualTo("isDelete", 0);

        if (StrUtil.isNotBlank(roleName)) criteria.andLike("name", roleName + "%");
        if (Objects.nonNull(status)) criteria.andEqualTo("status", status);
        if (Objects.nonNull(createTimeStart)) criteria.andBetween("createTime", createTimeStart, createTimeEnd);

        Page<Object> page = PageMethod.startPage(roleQuery.getPageNum(), roleQuery.getPageSize());
        List<UaaRoleInfo> roleInfoList = roleInfoDao.selectByExample(example);
        if (CollectionUtils.isEmpty(roleInfoList)) {
            return Response.yes(Paging.empty());
        }
        //查询角色关联权限
        List<Long> roleIdList = roleInfoList.stream().map(UaaRoleInfo::getId).collect(Collectors.toList());
        example = new Example(UaaRoleFunction.class);
        example.createCriteria().andIn("roleId", roleIdList);
        List<UaaRoleFunction> roleFunctionList = roleFunctionDao.selectByExample(example);

        Map<Long, List<UaaRoleFunction>> rfMap = roleFunctionList.stream().collect(Collectors.groupingBy(UaaRoleFunction::getRoleId));
        Map<Long, List<Long>> roleFunctionIdMap = new HashMap<>(rfMap.size());
        rfMap.forEach((rid, rf) -> roleFunctionIdMap.put(rid, rf.stream().map(UaaRoleFunction::getFunctionId).collect(Collectors.toList())));

        List<RoleInfoDTO> roleInfoDTOList = new ArrayList(roleInfoList.size());
        roleInfoList.forEach(roleInfo -> {
            RoleInfoDTO roleInfoDTO = roleInfoConvert.do2dto(roleInfo);
            roleInfoDTO.setFunctionIds(roleFunctionIdMap.get(roleInfoDTO.getId()));
            roleInfoDTOList.add(roleInfoDTO);
        });
        return Response.yes(new Paging<>(page.getTotal(), roleInfoDTOList));
    }


}
